package httplib;

import com.android.volley.Response;
import com.android.volley.VolleyError;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by niraymond on 16/8/22.
 */
public class VolleyHttpHelper<T> implements HttpHelper<T> {
    //服务URL地址
    private static String serviceURL;
    //Token 签名
    private static String serviceToken;

    private Map<String, String> postBody;
    private String bodySign;

    @Override
    public void PostJson(Class<T> clazz, Object object, final HttpProxyListener l) {
        try {
            if(signBody(object) == null){
                l.NetError(0, "请求参数签名错误!");
                return;
            }
        } catch (InvocationTargetException e) {
            l.NetError(0, "请求参数错误" + e.toString());
        } catch (IllegalAccessException e) {
            l.NetError(0, "请求参数错误" + e.toString());
        } catch (NoSuchMethodException e) {
            l.NetError(0, "请求参数错误" + e.toString());
        }

        VolleyRequest request = new VolleyRequest(serviceURL, clazz, null, postBody,
                new Response.Listener() {
                    @Override
                    public void onResponse(Object response) {
                        l.Success(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                        l.NetError(0, error.getMessage());
                    }
                }
        );

        VolleyRequestQueue.getInstance().addToRequestQueue(request);
    }

    private String signBody(Object object) throws NoSuchMethodException,
            InvocationTargetException, IllegalAccessException {
        if(object instanceof  BaseInput){
            TreeMap<String, String> needSignMap = new TreeMap<>();

            //解析参数，放入TreeMap排序
            BaseInput input = (BaseInput) object;
            HashMap<String, Method> map = input.getProperties();
            for (String key :map.keySet()){
                Method method = map.get(key);
                String result = (String) method.invoke(input, null);
                needSignMap.put(key, result);
            }
            needSignMap.putAll(input.getData());
            postBody = needSignMap;

            //组合待签名字符串
            bodySign = "";
            for (String key :postBody.keySet()){
                bodySign +=key;
                bodySign += postBody.get(key);
            }

            //签名
            bodySign = Md5.getMD5(bodySign).toUpperCase();
            bodySign += serviceToken;
            bodySign = Md5.getMD5(bodySign).toUpperCase();
            postBody.put("sign", bodySign);

            return bodySign;
        }
        return null;
    }

    public static synchronized void setServiceURL(String url){
        serviceURL = url;
    }

    public static String getServiceURL(){
        return serviceURL;
    }

    public static String getServiceToken() {
        return serviceToken;
    }

    public static void setServiceToken(String serviceToken) {
        VolleyHttpHelper.serviceToken = serviceToken;
    }
}
